package com.jzo2o.trade.service.impl;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.jzo2o.api.trade.dto.response.PayResDTO;
import com.jzo2o.api.trade.enums.PayTypeEnum;
import com.jzo2o.trade.config.WxPayConfig;
import com.jzo2o.trade.config.WxPayProperties;
import com.jzo2o.trade.model.domain.Trading;
import com.jzo2o.trade.service.IWxPayService;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import com.wechat.pay.java.service.payments.nativepay.model.Amount;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * \* User: chy
 * \* Date: 2025/10/8  15:13
 * \* Description:
 * \
 */
@Service
@Slf4j
public class WxPayServiceImpl implements IWxPayService {
    @Resource
    private WxPayProperties wxPayProperties;
    @Resource
    private WxPayConfig wxPayConfig;

    @Override
    public PayResDTO pay(Trading trading) {

        PayResDTO resDTO = null;
        // 1.判断微信的支付类型  todo:这里比较的时候注意, 枚举的code是Integer,而数据库的trading_type是String,所以需要转换一下.
        if (PayTypeEnum.NATIVE.getValue().equals(trading.getTradingType())) {
            log.info("微信native方式---扫码支付开始");
            resDTO= nativePay( trading);
            return resDTO;
        }
        return resDTO;
    }

    private PayResDTO nativePay(Trading trading) {
        // 1.创建支付服务
        NativePayService payService = new NativePayService.Builder().config(wxPayConfig.config()).build();
        // 2.构造预支付请求参数
        PrepayRequest prepayRequest = new PrepayRequest();
        prepayRequest.setAppid(wxPayProperties.getAppId());
        prepayRequest.setMchid(wxPayProperties.getMerchantId());
        prepayRequest.setDescription(trading.getMemo());
        // 支付交易单号
        prepayRequest.setOutTradeNo(trading.getTradingOrderNo());
        // 金额 todo:注意微信的金额单位是分,所以需要转换一下.
        Amount amount = new Amount();
        amount.setTotal(trading.getTradingAmount().multiply(BigDecimal.valueOf(100)).intValue());
        // 货币
        amount.setCurrency("CNY");
        prepayRequest.setAmount(amount);
        // 回调地址
        prepayRequest.setNotifyUrl(wxPayProperties.getNotifyUrl());

        // 3.调用微信支付服务
        PrepayResponse result = payService.prepay(prepayRequest);
        log.info("微信扫码支付结果:{}",result);


        // 二维码链接转换为图片,转换为base64
        String codeUrl = result.getCodeUrl();
        QrConfig qrConfig = new QrConfig();
        qrConfig.setWidth(300);
        qrConfig.setHeight(300);
        String base64 = QrCodeUtil.generateAsBase64(codeUrl, qrConfig, ImgUtil.IMAGE_TYPE_JPG);
        // 4.封装返回结果
        PayResDTO resDTO = new PayResDTO();
        resDTO.setQrCode(base64);
        resDTO.setPayOrderNo(Long.parseLong(trading.getTradingOrderNo()));
        resDTO.setPlaceOrderMsg("微信扫码支付成功");
        return resDTO;
    }
}
